<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	//$pet_id = $_REQUEST["pet_id"];

	$utility_id = $_REQUEST["utility_id"];


	$db = new myDBS;
	$Cur = new Currency;

	$sql= "SELECT DISTINCT tbl_utility.*, expenses1_name, expenses2_name, panngan2_name, panngan3_name, panngan2.panngan2_id, panngan3.panngan3_id, expenses_lvl2.e_id FROM tbl_utility, expenses_lvl1, expenses_lvl2, panngan2, panngan3  WHERE utility_id  = '$utility_id' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and panngan2.panngan2_id = tbl_utility.panngan2_id and panngan3.panngan3_id = tbl_utility.panngan3_id";
	//echo $sql;
	$result = $db->runSQL($sql);
	$rows = $db->fetch_object ( $result );

	$elass = gen_account_id($rows->panngan2_id, $rows->panngan3_id, $rows->e_id);
// just require TCPDF instead of FPDF

	//define('FPDF_FONTPATH','fpdf/font/');
	require_once('fpdf/TCPDF/tcpdf.php');
	require_once('fpdf/FPDI/fpdi.php');

	class PDF extends FPDI {
		/**
		 * "Remembers" the template id of the imported page
		 */
		var $_tplIdx;
		
		/**
		 * include a background template for every page
		 */
		function Header() {
			if (is_null($this->_tplIdx)) {
				$this->numPages =$this->setSourceFile('pdf_form/petition1.pdf');
				
				$this->_tplIdx = $this->importPage(1);
			}
			$this->useTemplate($this->_tplIdx);
			
			/*
			//$this->AddFont('THSarabunNew','','THSarabunNew.php');
			//$this->SetFont('THSarabunNew', '', 9);
			$this->SetTextColor(255);
			$this->SetXY(60.5, 24.8);
			$this->Cell(0, 8.6, "TCPDF and FPDI");
			*/
		}
		
		function Footer() {}
	}

	// initiate PDF
	$pdf = new PDF($orientation='P', $unit='mm', "A4", $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false);
	$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
	$pdf->SetAutoPageBreak(true, 40);
	$pdf->setFontSubsetting(false);

	// add a page
	$pdf->AddPage();

	//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
	//$pdf->SetFont("freeserif", "", 12);
	$pdf->SetFont('thsarabunb','B',15);
	// now write some text above the imported page
	$pdf->SetXY(30, 27);
	$pdf->Write(0, $_SESSION["lvl1"]);

	$pdf->SetXY(28, 33);
	$pdf->Write(0, $rows->budget_year);

	$pdf->SetXY(92, 34);
	$pdf->Write(0, $rows->pet_id);

	$pdf->SetXY(24, 41);
	$pdf->Write(0, $rows->panngan2_name);
	
	$pdf->SetXY(160, 48.5);
	$pdf->Write(0, $elass);

	$pdf->SetXY(84, 41);
	$pdf->Write(0, $rows->panngan3_name);

	$pdf->SetXY(155, 56);
	$pdf->Write(0, $elass);


	$pdf->SetXY(28, 47);
	$pdf->MultiCell(50, 10, $rows->expenses1_name, 0, '',0, 0, '', '', true);

	$pdf->SetXY(90, 47);
	$pdf->Write(0, $rows->expenses2_name);

	$pdf->SetXY(90, 55);
	$pdf->Write(0, $rows->expenses3_name);

	$pdf->SetXY(76, 69);
	$pdf->Cell(41,0,number_format($rows->utility, 2),0,0,'R');

	$pdf->SetXY(120, 69);
	$pdf->MultiCell(90, 45,  $_SESSION["lvl2"]. " ประจำ ". $rows->month_year, 0, '', 0, 1, '', '', true);


	$pdf->SetXY(76, 110);
	$pdf->Cell(41,0,number_format($rows->utility, 2),0,0,'R');

	$pdf->SetXY(56, 115);
	$pdf->Write(0, $Cur->bahtThai($rows->utility));


	$pdf->SetXY(22, 171);
	$pdf->Cell(39,0,number_format($rows->utility, 2),0,0,'C');

	$pdf->SetXY(90, 171);
	$pdf->Cell(39,0,number_format($rows->utility, 2),0,0,'C');

	$pdf->SetXY(157, 171);
	$pdf->Cell(39,0,number_format($rows->utility, 2),0,0,'C');

	$pdf->SetXY(29, 232);
	$pdf->Cell(36,0,number_format($rows->utility, 2),0,0,'C');

	$pdf->SetXY(73, 232);
	$pdf->Write(0, $Cur->bahtThai($rows->utility));


	$pdf->Output('output_petition1.pdf', 'I');
?>